package club.ljwang.service;

import club.ljwang.dao.PermissionMapper;
import club.ljwang.entity.PageResult;
import club.ljwang.entity.QueryPageBean;
import club.ljwang.exception.CheckItemException;
import club.ljwang.exception.PermissionException;
import club.ljwang.pojo.CheckItem;
import club.ljwang.pojo.Permission;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: wangl
 * @Date: 2020/3/9 11:39
 * @Description:
 */

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //1.告诉Mybatis我们要分页
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //2.写一个跟分页毫无关系的sql
        List<Permission> permissions = permissionMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),permissions);
    }

    @Override
    public void delete(Integer id) throws PermissionException{
        Integer count = permissionMapper.findCountByPermissionId(id);
        if (count>0) {

            throw new PermissionException("角色权限组存在此权限项引用,引用ID为"+id);

        }else {

            permissionMapper.delete(id);

        }
    }

    @Override
    public Permission findById(Integer id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        return permission;
    }

    @Override
    public void edit(Permission permission) {
        permissionMapper.updateByPrimaryKeySelective(permission);
    }

    @Override
    public void add(Permission permission) {
        permissionMapper.insert(permission);
    }
}
